package com.bed.lovewithpatientbed.controller;

import com.bed.lovewithpatientbed.common.bean.LayUiSideMenuBean;
import com.bed.lovewithpatientbed.pojo.Menu;
import com.bed.lovewithpatientbed.pojo.Permission;
import com.bed.lovewithpatientbed.pojo.RolePermission;
import com.bed.lovewithpatientbed.pojo.WorkerMessage;
import com.bed.lovewithpatientbed.service.IMenuService;
import com.bed.lovewithpatientbed.service.IPermissionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import com.bed.lovewithpatientbed.common.bean.LayUiSideMenuBean;
import com.bed.lovewithpatientbed.service.IPermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuxiaoding
 * @version 1.0.0
 * @ClassName ViewController.java
 * @Description 负责页面跳转一个Controller
 */
@Controller
public class ViewController {

    private IPermissionService permissionService;

    private IMenuService menuService;

    @Autowired
    public void setPermissionService(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @Autowired
    public void setMenuService(IMenuService menuService) {
        this.menuService = menuService;
    }

    @RequestMapping(value = "/mainPage", method = RequestMethod.GET)
    public String mainPage() {
        return "main";
    }

    @RequestMapping(value = "/userPage", method = RequestMethod.GET)
    public String userPage() {
        System.out.println("ViewController.userPage");
        return "user/table";
    }

    @RequestMapping("/layuiSideUrl")
    @ResponseBody
    public LayUiSideMenuBean layuiSide() {
        System.out.println("ViewController.layuiSide");
        LayUiSideMenuBean layUiSideMenuBean = new LayUiSideMenuBean();


        // 获取当前主体
        Subject subject = SecurityUtils.getSubject();
        // 获取当前主体对应WorkerMessage
        WorkerMessage workerMessage = (WorkerMessage) subject.getPrincipal();
        if (workerMessage == null) {
            layUiSideMenuBean.setCode(-1);
            layUiSideMenuBean.setMsg("无用户登录");
            return layUiSideMenuBean;
        }

        // 根据 roleId 在 role_permission 表中查询 role_permission，用于获取 permissionId
        List<RolePermission> rolePermissionList = permissionService.getRolePermissionByRoleId(workerMessage.getRoleId());
        if (rolePermissionList == null || rolePermissionList.size() < 1) {
            layUiSideMenuBean.setCode(-2);
            layUiSideMenuBean.setMsg("该人员无任何权限");
            return layUiSideMenuBean;
        }

        // 提取其中permissionId
        List<Integer> permissionIds = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissionList) {
            permissionIds.add(rolePermission.getPermissionId());
        }
        // 查询这些permissionId对应permission
        List<Permission> permissionList = permissionService.getPermissionByPermissionId(permissionIds);
        if (permissionList == null || permissionList.size() < 1) {
            layUiSideMenuBean.setCode(-3);
            layUiSideMenuBean.setMsg("获取该人员权限错误");
            return layUiSideMenuBean;
        }

        List<LayUiSideMenuBean.DataDTO> data = new ArrayList<>();
        layUiSideMenuBean.setData(data);

        // 添加主页
        LayUiSideMenuBean.DataDTO mainPage = new LayUiSideMenuBean.DataDTO();
        mainPage.setTitle("主页");
        mainPage.setIcon("layui-icon-home");
        mainPage.setJump("/");
        data.add(mainPage);

        // 根据有无 parent_id 对查询到分类 一级菜单，二级菜单
        List<Permission> firLevMenu = new ArrayList<>();
        List<Permission> secLevMenu = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (permission.getParentId() == null) {
                firLevMenu.add(permission);
            } else {
                secLevMenu.add(permission);
            }
        }

        // 添加一级菜单
        for (Permission permission : firLevMenu) {
            LayUiSideMenuBean.DataDTO firLevDTO = new LayUiSideMenuBean.DataDTO();
            // 根据 permission 中 parent_id 在 menu 表中查询对应 Menu
            Menu menu = menuService.retrieveMenuByMenuId(permission.getIsmenu());
            if (menu != null) {
                firLevDTO.setTitle(menu.getMenuName());
                firLevDTO.setIcon(menu.getMenuIcon());
            }
            firLevDTO.setJump(permission.getUrl());
            data.add(firLevDTO);
        }

        // 获取二级菜单对应一级菜单
        Map<Integer, Menu> firstMenuMap = new HashMap<>();
        for (Permission permission : secLevMenu) {
            // 查询二级菜单对应一级菜单
            Permission parentPermission = permissionService.getPermissionByPermissionId(permission.getParentId());
            if (parentPermission != null) {
                Menu menu = menuService.retrieveMenuByMenuId(parentPermission.getIsmenu());
                firstMenuMap.put(parentPermission.getPermissionId(), menu);
            }
        }
        // Menu 准换为 LayUiSideMenuBean.DataDTO
        Map<Integer, LayUiSideMenuBean.DataDTO> firstMenuDTOMap = new HashMap<>();
        for (Integer integer : firstMenuMap.keySet()) {
            Menu menu = firstMenuMap.get(integer);
            LayUiSideMenuBean.DataDTO dataDTO = new LayUiSideMenuBean.DataDTO();
            dataDTO.setTitle(menu.getMenuName());
            dataDTO.setIcon(menu.getMenuIcon());
            dataDTO.setList(new ArrayList<LayUiSideMenuBean.DataDTO.ListDTO>());
            firstMenuDTOMap.put(integer,dataDTO);
            data.add(dataDTO);
        }

        // 添加二级菜单
        for (Permission permission : secLevMenu) {
            LayUiSideMenuBean.DataDTO.ListDTO listDTO = new LayUiSideMenuBean.DataDTO.ListDTO();
            Menu menu = menuService.retrieveMenuByMenuId(permission.getIsmenu());
            listDTO.setTitle(menu.getMenuName());
            listDTO.setJump(permission.getUrl());
            firstMenuDTOMap.get(permission.getParentId()).getList().add(listDTO);
        }

        // 正常流程
        layUiSideMenuBean.setCode(0);
        layUiSideMenuBean.setMsg("success");

        return layUiSideMenuBean;
    }

}
